package cn.iocoder.yudao.module.pickbook.controller.admin.collection;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.pickbook.controller.admin.collection.vo.*;
import cn.iocoder.yudao.module.pickbook.dal.dataobject.collection.CollectionDO;
import cn.iocoder.yudao.module.pickbook.service.collection.CollectionService;

@Tag(name = "管理后台 - 收藏")
@RestController
@RequestMapping("/pickbook/collection")
@Validated
public class CollectionController {

    @Resource
    private CollectionService collectionService;

    @PostMapping("/create")
    @Operation(summary = "创建收藏")
    @PreAuthorize("@ss.hasPermission('pickbook:collection:create')")
    public CommonResult<Long> createCollection(@Valid @RequestBody CollectionSaveReqVO createReqVO) {
        return success(collectionService.createCollection(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新收藏")
    @PreAuthorize("@ss.hasPermission('pickbook:collection:update')")
    public CommonResult<Boolean> updateCollection(@Valid @RequestBody CollectionSaveReqVO updateReqVO) {
        collectionService.updateCollection(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除收藏")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pickbook:collection:delete')")
    public CommonResult<Boolean> deleteCollection(@RequestParam("id") Long id) {
        collectionService.deleteCollection(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除收藏")
                @PreAuthorize("@ss.hasPermission('pickbook:collection:delete')")
    public CommonResult<Boolean> deleteCollectionList(@RequestParam("ids") List<Long> ids) {
        collectionService.deleteCollectionListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得收藏")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pickbook:collection:query')")
    public CommonResult<CollectionRespVO> getCollection(@RequestParam("id") Long id) {
        CollectionDO collection = collectionService.getCollection(id);
        return success(BeanUtils.toBean(collection, CollectionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得收藏分页")
    @PreAuthorize("@ss.hasPermission('pickbook:collection:query')")
    public CommonResult<PageResult<CollectionRespVO>> getCollectionPage(@Valid CollectionPageReqVO pageReqVO) {
        PageResult<CollectionDO> pageResult = collectionService.getCollectionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CollectionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出收藏 Excel")
    @PreAuthorize("@ss.hasPermission('pickbook:collection:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCollectionExcel(@Valid CollectionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CollectionDO> list = collectionService.getCollectionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "收藏.xls", "数据", CollectionRespVO.class,
                        BeanUtils.toBean(list, CollectionRespVO.class));
    }

}